package com.oim.fx.ui.component;

import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.TextField;
import javafx.scene.layout.Region;

/**
 * @author: XiaHui
 * @date: 2017年5月24日 上午10:50:25
 */
public class SearchBox extends Region {

	private TextField textBox;
	private Button clearButton;

	public SearchBox() {
		String searchBoxCss = SearchBox.class.getResource("/resources/common/css/searchbox.css").toExternalForm();
        getStylesheets().add(searchBoxCss);
		setId("SearchBox");
		getStyleClass().add("search-box");
		setMinHeight(24);
		//setPrefSize(200, 24);
		setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);
		
		
        
		textBox = new TextField();
		textBox.setPromptText("Search");

		clearButton = new Button();
		clearButton.setVisible(false);
		getChildren().addAll(textBox, clearButton);
		clearButton.setOnAction((ActionEvent actionEvent) -> {
			textBox.setText("");
			textBox.requestFocus();
		});

		textBox.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
			clearButton.setVisible(textBox.getText().length() != 0);
		});
	}

	@Override
	protected void layoutChildren() {
		textBox.resize(getWidth(), getHeight());
		clearButton.resizeRelocate(getWidth() - 18, 6, 12, 13);
	}

	public String getText(){
		return textBox.getText();
	}
	
	public void setPromptText(String value){
		textBox.setPromptText(value);
	}

	public void setText(String value) {
		textBox.setText(value);
	}
}